package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class AttributeService implements IAttributeService {

    @Autowired
    private AttributeMapper attributeMapper;
    @Autowired
    private AttributeTemplateMapper attributeTemplateMapper;

    @Override
    public void addNew(AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理【添加属性】的业务，参数：{}", attributeAddNewDTO);
        Long TemplateId = attributeAddNewDTO.getTemplateId();
        String name = attributeAddNewDTO.getName();
        //检查这个属性模版中是否存在这个名字的属性
        int count = attributeMapper.countByNameAndTemplateId(name, TemplateId);
        if (count > 0){
            String message = "添加属性失败,此属性模版中已存在这个名称的属性";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查是否存在这个属性模版
        Object counts = attributeTemplateMapper.getStandardById(TemplateId);
        if (counts == null){
            String message = "添加失败,属性模版不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO,attribute);
        attributeMapper.insert(attribute);



    }

    @Override
    public AttributeStandardVO selectById(Long id){
        log.debug("开始处理根据id查看详情的服务");
        AttributeStandardVO standardById = attributeMapper.getStandardById(id);
        if (standardById == null){
            String message = "别瞎查,没有这个id";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardById;
    }

    @Override
    public void update(AttributeUpdateDTO attributeUpdateDTO){
        log.debug("开始修改详情");

        String name = attributeUpdateDTO.getName();
        int rows = attributeMapper.countByName(name);
        if (rows >= 1){
            String message = "修改失败,名字已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeUpdateDTO,attribute);
        rows = attributeMapper.updateById(attribute);
        if (rows != 1){
            String message = "修改失败,服务器繁忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
    }

    @Override
    public List<AttributeListItemVO> selectByTemplateId(Long TemplateId){
        log.debug("开始处理根据属性模板id查询属性列表业务");
        List<AttributeListItemVO> attributeListItemVOS = attributeMapper.listByTemplateId(TemplateId);

        return attributeListItemVOS;
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理根据id删除属性详情");
        AttributeStandardVO standardById = attributeMapper.getStandardById(id);
        if (standardById == null ){
            String message = "删除失败,没有这个属性";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int rows = attributeMapper.deleteById(id);
        if (rows!=1){
            String message = "删除失败,服务器繁忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
